﻿using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>Represents a read-only pair of 2 values.</summary>
/// <remarks>System.Collections.Generic.KeyValuePair&lt;&gt; is ~the same,
/// however the name is much longer.</remarks>
/// <typeparam name="tFirst">Type of the first element</typeparam>
/// <typeparam name="tSecond">Type of the second element</typeparam>
public struct pair<tFirst, tSecond>
{
	/// <summary>The first value.</summary>
	public readonly tFirst first;

	/// <summary>The second value.</summary>
	public readonly tSecond second;

	/// <summary>__ctor</summary>
	/// <param name="f">The first value</param>
	/// <param name="s">The second value</param>
	public pair( tFirst f, tSecond s )
	{
		first = f;
		second = s;
	}

	// For some reasons, DropDownList control wants the "ValueMember" and "DisplayMember" members to be properties, not fields.
	// Here are your properties, Windows Forms.

	/// <summary>The first value.</summary>
	public tFirst fieldFirst { get { return first; } }
	/// <summary>The second value.</summary>
	public tSecond fieldSecond { get { return second; } }
};

/// <summary>This static class contains a few utility methods.</summary>
public static partial class EsentSerializationUtils
{
	/// <summary>Select two values from each element in the input sequence.
	/// Return the sequence of the selected pairs. </summary>
	/// <param name="src">Source sequence</param>
	/// <param name="selectFirst">first selector</param>
	/// <param name="selectSecond">second selector</param>
	/// <returns>Sequence of pairs</returns>
	public static IEnumerable<pair<tFirst, tSecond>>
		SelectPairs<tSource, tFirst, tSecond>( this IEnumerable<tSource> src,
			Func<tSource, tFirst> selectFirst, Func<tSource, tSecond> selectSecond )
	{
		return src.Select( elt => new pair<tFirst, tSecond>( selectFirst( elt ), selectSecond( elt ) ) );
	}
}